import logging

import config
import requests


class RegLoginApi(object):
    """
    功能：登录注册接口
    """

    def __init__(self, session):
        self.session1 = session

    # 获取图片验证码接口
    def get_img_verify_code(self, r):
        """
        :param r: 随机图片Id
        :return: 响应结果
        """
        re = self.session1.get(url=config.BASE_HOST + '/common/public/verifycode1/' + r)

        return re

    # 获取短信验证码接口
    def get_phone_verify_code(self, body):
        """

        :param body: 请求体
        :return: 响应数据
        """
        re = self.session1.post(url=config.BASE_HOST + '/member/public/sendSms', data=body)
        logging.info('获取注册短信验证码的响应体数据：{}'.format(re.json()))
        return re

    # 注册接口
    def user_register(self, body):
        """

        :param body: 注册的请求体
        :return: 返回响应结果
        """
        re = self.session1.post(url=config.BASE_HOST + '/member/public/reg', data=body)
        logging.info('获取注册的响应体数据：{}'.format(re.json()))
        return re

    # 登录接口
    def user_login(self, body):
        """

        :param body: 登录请求Body
        :return: 响应结果
        """
        re = self.session1.post(url=config.BASE_HOST + '/member/public/login', data=body)
        logging.info('获取登录的响应体数据：{}'.format(re.json()))
        return re


if __name__ == '__main__':
    R = RegLoginApi(session=requests.session())
    R.get_img_verify_code(r='3')
    R.get_phone_verify_code(body={"phone": "18708309000",
                                  "imgVerifyCode": "8888",
                                  "type": "reg"})
    R.user_register(body={"phone": "18708309000", "password": "123456a",
                          "verifycode": "8888",
                          "phone_code": "666666",
                          "dy_server": "on"})

    R.user_login(body={"keywords": "18708309000", "password": "123456a"})
